'use strict';

/**
 * @name ProcessListCtrl
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('ProcessDetailCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaEdit', function($scope, $rootScope, $state, $stateParams, MetaResources, MetaRemind, MetaEdit) {
    $scope.editCtrl = {
        data: {}
    };
    $scope.init = function() {
        MetaEdit.initEditModule($scope, $stateParams, 'process', 'demandSideProcess,technologySideProcess,demand,demand.publisher,demand.leader,demand.projectManager,demand.verifier,technology.verifier,technology,technology.publisher,technology.leader,technology.projectManager,projectManager');
        //跳转步骤
        $scope.changeStep = function(step, state) {
            if ($scope.editCtrl.stateCompare[$scope.editCtrl.data.state] >= step) {
                $state.go(state);
            } else {
                MetaRemind.errorInfo("失败", "还未进行到该步骤");
            }
        };
        //
        angular.element('.demandSide').css('height', '600');
    };
    //更新进程的各个阶段的信息
    $scope.updateProcess = function(sideType, updateType) {
        var method = (sideType === 'demand' ? 'updateDemand' : 'updateTech');
        var id = (sideType === 'demand' ? $scope.editCtrl.data.demandSideProcess.id : $scope.editCtrl.data.technologySideProcess.id);
        var postData = {};
        switch (updateType) {
            case 'demandContact':
                postData.checkTechnologySideAt = new Date();
                break;
            case 'techContact':
                postData.checkDemandSideAt = new Date();
                break;
            case 'sendInvitation':
                postData.inviteTechnologySideAt = new Date();
                break;
            case 'agreeInvitation':
                postData.agreeWithInvitingAt = new Date();
                break;
            case 'applyForReimbursement':
                postData.applyForReimbursementAt = new Date();
                postData.bank = $scope.editCtrl.data.technologySideProcess.bank;
                postData.bankAccount = $scope.editCtrl.data.technologySideProcess.bankAccount;
                break;
            case 'agreeReimbursement':
                postData.agreeWithReimbursementAt = new Date();
                break;
            case 'applyForContract':
                postData = {
                    'signAContractAt': (new Date()),
                    'contract': $scope.editCtrl.data.demandSideProcess.contract,
                    'bank': $scope.editCtrl.data.demandSideProcess.bank,
                    'bankAccount': $scope.editCtrl.data.demandSideProcess.bankAccount,
                    'starToDemandProvider': $scope.editCtrl.data.demandSideProcess.starToDemandProvider,
                    'starToSolutionProcessProvider': $scope.editCtrl.data.demandSideProcess.starToSolutionProcessProvider
                };
                break;
            case 'agreeForContract':
                postData = {
                    'confirmContractAt': (new Date()),
                    'starToDemandProvider': $scope.editCtrl.data.technologySideProcess.starToDemandProvider,
                    'starToProjectLeader': $scope.editCtrl.data.technologySideProcess.starToProjectLeader,
                    'starToPlatform': $scope.editCtrl.data.technologySideProcess.starToPlatform
                };
                break;
            default:
                break;
        }
        MetaResources.process[method]({
                'id': id
            }, postData,
            function() {
                MetaRemind.success("恭喜", "修改成功");
                $state.reload();
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };
    $scope.toBeContinued = MetaRemind.error;
    $scope.init();
}]);
